﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Twinkle.Dialogs.Config
{
    public partial class OuterAPI : Krile.Kernel.Control.ConfigPage
    {
        public OuterAPI()
        {
            InitializeComponent();
        }

        private void OuterAPI_Load(object sender, EventArgs e)
        {
            useUrlShortening.Checked = Kernel.Config.External.URLShoteningService.Use;
            useUrlShortenAsJIT.Checked = Kernel.Config.External.URLShoteningService.UseAsJIT;
            useOnlyExceeds.Checked = Kernel.Config.External.URLShoteningService.UseOnlyExceeds;
            urlShortenServiceSelector.Items.AddRange(Kernel.Config.External.URLShoteningService.GetServicesStrings());
            urlShortenServiceSelector.SelectedIndex = (int)Kernel.Config.External.URLShoteningService.Provider;
            urlShortenID.Text = Kernel.Config.External.URLShoteningService.ID;
            urlShortenAPIKey.Text = Kernel.Config.External.URLShoteningService.PW;
            urlShortenResolveURL.Checked = Kernel.Config.External.URLShoteningService.AutoResolveURL;

            UseImageEmbedding.Checked = Kernel.Config.External.ImageEmbeddingService.UseImageEmbedding;
            ImageEmbServiceSelector.SelectedIndex = (int)Kernel.Config.External.ImageEmbeddingService.EmbeddingService;
            ImageEmbId.Text = Kernel.Config.External.ImageEmbeddingService.ID;
            ImageEmbPass.Text = Kernel.Config.External.ImageEmbeddingService.PW;

            extractEmbeddedImage.Checked = Kernel.Config.External.ImageEmbeddingService.ExtractEmbedded;
            useUrlShortening_CheckedChanged(null, null);
        }

        public override void Apply()
        {
            Kernel.Config.External.URLShoteningService.Use = useUrlShortening.Checked;
            Kernel.Config.External.URLShoteningService.UseAsJIT = useUrlShortenAsJIT.Checked;
            Kernel.Config.External.URLShoteningService.UseOnlyExceeds = useOnlyExceeds.Checked;
            Kernel.Config.External.URLShoteningService.Provider = (Data.Config.ExternalConfig.URLShortenServiceDescriptor.URLShortenServiceProviders)urlShortenServiceSelector.SelectedIndex;
            Kernel.Config.External.URLShoteningService.ID = urlShortenID.Text;
            Kernel.Config.External.URLShoteningService.PW = urlShortenAPIKey.Text;
            Kernel.Config.External.URLShoteningService.AutoResolveURL = urlShortenResolveURL.Checked;

            Kernel.Config.External.ImageEmbeddingService.UseImageEmbedding = UseImageEmbedding.Checked;
            Kernel.Config.External.ImageEmbeddingService.EmbeddingService = (Data.Config.ExternalConfig.ImageEmbeddingServiceDescriptor.ImageEmbeddingServices)ImageEmbServiceSelector.SelectedIndex;
            Kernel.Config.External.ImageEmbeddingService.ID = ImageEmbId.Text;
            Kernel.Config.External.ImageEmbeddingService.PW = ImageEmbPass.Text;
            Kernel.Config.External.ImageEmbeddingService.ExtractEmbedded = extractEmbeddedImage.Checked;
        }

        private void useUrlShortening_CheckedChanged(object sender, EventArgs e)
        {
            useUrlShortenAsJIT.Enabled = useUrlShortening.Checked;
            urlShortenServiceSelector.Enabled = useUrlShortening.Checked;
            urlShortenID.Enabled = useUrlShortening.Checked && urlShortenServiceSelector.SelectedIndex > 0;
            urlShortenAPIKey.Enabled = useUrlShortening.Checked && urlShortenServiceSelector.SelectedIndex > 0;
        }

        private void urlShortenServiceSelector_SelectedIndexChanged(object sender, EventArgs e)
        {
            useUrlShortening_CheckedChanged(sender, e);
        }

        private void UseImageEmbedding_CheckedChanged(object sender, EventArgs e)
        {
            ImageEmbServiceSelector.Enabled = UseImageEmbedding.Checked;
            ImageEmbId.Enabled = UseImageEmbedding.Checked && ImageEmbServiceSelector.SelectedIndex != 2;
            ImageEmbPass.Enabled = UseImageEmbedding.Checked && ImageEmbServiceSelector.SelectedIndex != 2;
        }

        private void ImageEmbServiceSelector_SelectedIndexChanged(object sender, EventArgs e)
        {
            UseImageEmbedding_CheckedChanged(sender, e);
        }

    }
}
